package com.swind.fast.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;

import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import com.swind.fast.common.interceptor.UserAppCodeInterceptor;

import io.swagger.annotations.ApiOperation;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
// import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


@Configuration
@EnableSwagger2
@EnableKnife4j
// @EnableSwaggerBootstrapUi
@Import(BeanValidatorPluginsConfiguration.class)
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
public class Swagger2Config implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Bean(value = "defaultApi2")
    public Docket defaultApi2() {
        return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo())
            .select()
            //此包路径下的类，才生成接口文档
            // .apis(RequestHandlerSelectors.basePackage("com.swind.fast"))
            // .apis(RequestHandlerSelectors.basePackage("com.swind.fast.modules.app.wii"))
            .apis(RequestHandlerSelectors.basePackage("com.swind.fast.modules"))
            // .apis(RequestHandlerSelectors.basePackage("com.swind.fast.modules.system"))
            //加了ApiOperation注解的类，才生成接口文档
            .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
            .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
            .paths(PathSelectors.any())
            .build();
            // .securitySchemes(Collections.singletonList(securityScheme()))
            // .securityContexts(securityContexts());
            //.globalOperationParameters(setHeaderToken());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
            // //大标题
            .title("后台服务API接口文档")
            // 版本号
            .version("1.0")
//              .termsOfServiceUrl("NO terms of service")
            // 描述
            .description("后台API接口")
            // 作者
            // .contact("Swind")
            .license("The Swind License")
            .licenseUrl("")
            .build();
    }

}